/*
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.beiuu.myfirstandroidapp;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.beiuu.myfirstandroidapp.Frag.BookFragment;
import com.beiuu.myfirstandroidapp.Frag.NewsFragment;

public class BottomNavigationBarActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    BottomNavigationBar mBottomNavigationBar;
    FrameLayout mFrameLayout;


    private BookFragment mBookFragment = BookFragment.newInstance();
    private NewsFragment mNewsFragment = NewsFragment.newInstance();
    private Fragment lastFragment = null ;
//    private UserFragment mUserFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation_bar);


        InitNavigationBar();

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    private void InitNavigationBar() {
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

        mBottomNavigationBar.setTabSelectedListener(this);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.man, "图书").setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.drawable.man, "新闻").setActiveColorResource(R.color.teal))
                .addItem(new BottomNavigationItem(R.drawable.man, "设置").setActiveColorResource(R.color.blue))
                .setFirstSelectedPosition(0)
                .initialise();

        setDefaultFragment();
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        mBookFragment = BookFragment.newInstance();
        transaction.add(R.id.fragment_container, mBookFragment);
        transaction.commit();

        lastFragment = mBookFragment;
    }


    @Override
    public void onTabSelected(int position) {
        Log.d("onTabSelected", "onTabSelected: " + position);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:

//                transaction.replace(R.id.fragment_container, mBookFragment);

                if(mBookFragment.isAdded()){
                    transaction.hide(lastFragment).show(mBookFragment);
                }else{
                    transaction.hide(lastFragment).add(R.id.fragment_container,mBookFragment);
                }

                lastFragment = mBookFragment;
                break;
            case 1:

                //transaction.replace(R.id.fragment_container, mNewsFragment);

                if(mNewsFragment.isAdded()){
                    transaction.hide(lastFragment).show(mNewsFragment);
                }else{
                    transaction.hide(lastFragment).add(R.id.fragment_container,mNewsFragment);
                }

                lastFragment = mNewsFragment;
                break;

            default:
                break;
        }
        // 事务提交
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {
        Log.d("onTabUnselected", "onTabUnselected: " + position);
    }

    @Override
    public void onTabReselected(int position) {
        Log.d("onTabReselected", "onTabReselected: " + position);
    }
}
